﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Ruici.Web.Services;
using System.Data;
using Ruici.Web.Services.ServiceForWeb;
using System.Security.Cryptography;
using System.Configuration;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Ruici.BLL
{
    public class UserBll
    {
        private UserService userModel = new UserService();

        public WebAccount getUserExist(string sUserID, string sPassword)
        {
            DataSet ValidateResultDS = userModel.GetRegisteredWebUser(sUserID, sPassword);
            WebAccount webAccount = null;

            if (ValidateResultDS != null)
                webAccount = userModel.GetWebAccountByUserId(ValidateResultDS.Tables[0].Rows[0]["UserName"].ToString());

            return webAccount;
        }

        public Dictionary<int, string> Login(string sUserID, string sPassword)
        {
            string sLoginResult = userModel.Login(sUserID, sPassword);
            string sResult = string.Empty;
            Dictionary<int,string> sLoginResultDic = new Dictionary<int, string>();
            JObject jLoginResult = (JObject)JsonConvert.DeserializeObject(sLoginResult);
            sResult = jLoginResult["result"].ToString();
            switch(sResult)
            {
                case "1":
                    sLoginResultDic.Add(1, "手机号未注册！");
                    break;
                case "2":
                    sLoginResultDic.Add(2, "用户名不存在！");
                    break;
                case "3":
                    sLoginResultDic.Add(3, "账号与密码不匹配！");
                    break;
                case "4":
                    sLoginResultDic.Add(4, "无此用户！");
                    break;
                default:
                    sLoginResultDic.Add(0, "登录成功！");
                    break;
            }
            return sLoginResultDic;
        }

        public Dictionary<int,string> registNewUser(string sMobile, string sPassword)
        {
            Dictionary<int, string> RegistResultDic = new Dictionary<int, string>();

            string sResultCode = userModel.AddRegisteredWebUser(sMobile, "匿名", "2", string.Empty, "Clinic" + sMobile, sPassword);

            switch (sResultCode)
            {
                case "R995":
                    RegistResultDic.Add(-1,"未知错误");
                    break;
                case "R996":
                    RegistResultDic.Add(-1, "该手机号已被注册");
                    break;
                case "R998":
                    RegistResultDic.Add(-1, "注册参数不合法");
                    break;
                case "R999":
                    RegistResultDic.Add(-1, "该会员已有账号");
                    break;
                default:
                    RegistResultDic.Add(1, "注册成功");
                    break;
            }

            return RegistResultDic;
        }

        public bool updateUser(string sName, string sCertNo,int iPID, int sGender=-1)
        {
            bool bUpdateResult = false;
            string certTypeCode = "CID";
            string certNo = string.Empty;        

            Member member = userModel.GetMemberInforByPID(iPID);

            member.Name = sName;
            if(sGender != -1)
                member.Sex = sGender;
            certNo = sCertNo;

            int iResultCode = userModel.UpdateUserInforWithCert(member, certTypeCode, certNo);

            if (iResultCode == 1)
                bUpdateResult = true;

            return bUpdateResult;
        }

        public WebAccount getWebAccountByMobile(string sMobile)
        {
            return userModel.GetWebAccountByMobile(sMobile);
        }

        public string RetriveUserPassword(string sMobile)
        {
            return userModel.RetriveUserPassword(sMobile);
        }

        public Dictionary<int,string> changePassword(string sUserId,string sPassword)
        {
            Dictionary<int,string> sChangeResult = new Dictionary<int, string>();

            int iResultCode = userModel.ChangePassword(sUserId, sPassword);

            if (iResultCode == 0)
                sChangeResult.Add(-1, "接口异常");
            else if (iResultCode == 1)
                sChangeResult.Add(1,"密码修改成功，请妥善保存。");
            else
                sChangeResult.Add(-1,"参数不合法");

            return sChangeResult;
        }

        public void SendMessage(string sMobile, string sValidateCode)
        {
            string sMessage = ConfigurationManager.AppSettings["MessageContent"].ToString();
            
            userModel.SendSMSMessage(sMobile, string.Format(sMessage,sValidateCode));
        }
    }
}
